using System;
using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class BackgroundWidget : Widget {
        public static float Seed = new Random().Float(0f, 1000f);

        public static Vector2 Shift = new Random().Vector2(10000f);

        public static int LastAnimateFrame = -1;

        public static Random Random = new(0);

        public float ShiftSpeed = 2f;

        public float Density = 1f;

        public float Brightness = 0.75f;

        public BackgroundWidget() {
            IsDrawRequired = true;
            IsHitTestVisible = false;
        }

        public override void Draw(DrawContext dc) {
            Display.Clear(Color.Black, 1f);
            float num = 1.25f * GlobalTransform.M11;
            Random.Reset(0);
            if (Time.FrameIndex > LastAnimateFrame) {
                LastAnimateFrame = Time.FrameIndex;
                float num2 = (float)MathUtils.Remainder(Time.FrameStartTime + Seed, 10000.0);
                float x = (float)Math.PI * 2f * SimplexNoise.OctavedNoise(num2, 0.05f, 1, 2f, 0.5f);
                float num3 = ShiftSpeed * num * MathUtils.Lerp(0.5f, 1.5f, SimplexNoise.OctavedNoise(num2 + 1000f, 0.1f, 1, 2f, 0.5f));
                Shift += num3 * Time.FrameDuration * new Vector2(MathUtils.Sin(x), 0f - MathUtils.Cos(x));
            }
            TexturedBatch2D texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(Textures.Star, false, 0, null, null, BlendState.AlphaBlend);
            for (int i = 0; i < (int)(Density * 800f); i++) {
                float x2 = MathUtils.Remainder(Random.Float(0f, Window.Size.X) + Shift.X, Window.Size.X);
                float y = MathUtils.Remainder(Random.Float(0f, Window.Size.X) + Shift.Y, Window.Size.Y);
                Vector2 vector = new(x2, y);
                Vector2 vector2 = new(num * Random.Float(1f, 2.5f));
                float r = Random.Float(0.8f, 1f);
                float g = Random.Float(0.7f, 0.8f);
                int num4 = 1;
                float num5 = MathUtils.Sqr(Random.Float(0.1f, Brightness));
                texturedBatch2D.QueueQuad(vector - vector2, vector + vector2, 0f, Vector2.Zero, Vector2.One, num5 * new Color(r, g, num4));
            }
            TexturedBatch2D texturedBatch2D2 = dc.PrimitivesRenderer2D.TexturedBatch(
                Textures.StarTwinkle,
                false,
                0,
                null,
                null,
                BlendState.AlphaBlend
            );
            for (int j = 0; j < (int)(Density * 100f); j++) {
                float x3 = MathUtils.Remainder(Random.Float(0f, Window.Size.X) + Shift.X, Window.Size.X);
                float y2 = MathUtils.Remainder(Random.Float(0f, Window.Size.Y) + Shift.Y, Window.Size.Y);
                Vector2 vector3 = new(x3, y2);
                Vector2 vector4 = new(num * Random.Float(2f, 7f));
                float r2 = Random.Float(0.8f, 1f);
                float g2 = Random.Float(0.7f, 0.8f);
                int num6 = 1;
                int count = texturedBatch2D2.TriangleVertices.Count;
                texturedBatch2D2.QueueQuad(
                    vector3 - vector4,
                    vector3 + vector4,
                    0f,
                    Vector2.Zero,
                    Vector2.One,
                    0.8f * Brightness * new Color(r2, g2, num6)
                );
                texturedBatch2D2.TransformTriangles(Matrix.CreateRotationZ(Random.Float(0f, 6.28f)), count);
            }
        }
    }
}